package com.zhouyu.fileSync.FSclient.transmission.communicationBetweenClients.api.impl;

import com.zhouyu.fileSync.FSclient.ClientAppContext;
import com.zhouyu.fileSync.FSclient.transmission.communicationBetweenClients.api.CommunicationService;
import com.zhouyu.fileSync.FScommon.annotations.RpcService;
import com.zhouyu.fileSync.FScommon.commonUtils.FileUtils;
import com.zhouyu.fileSync.FScommon.entities.FSClient;

import java.io.File;

@RpcService
public class CommunicationServiceImpl implements CommunicationService {


    @Override
    public String getFileMD5FromTarget(String sourceFilePath, String oldMd5, Long lastModified, Long oldFileSize, String syncRule, FSClient target, FSClient fileReceiver) {
        if (ClientAppContext.CONFIGS.CLIENT.getClientName().equals(target.getClientName()) && ClientAppContext.CONFIGS.CLIENT.getClientPassword().equals(target.getClientPassword())) {
            File sourceFile = new File(sourceFilePath);
            try {
                String currentMd5 = FileUtils.getMd5(sourceFile);
                Long currentSize = FileUtils.getFileSize(sourceFile);
                Long currentModified = FileUtils.getFileLastModified(sourceFile);
                if (oldMd5 == null) {
                    return currentMd5;
                } else {
                    if (currentMd5.equals(oldMd5)) {
                        return null;
                    } else {
                        if (syncRule.startsWith("B")) {
                            if (currentSize<=oldFileSize){
                                return null;
                            }
                            if (syncRule.endsWith("N")) {
                                if (currentModified<=lastModified){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            } else if (syncRule.endsWith("O")) {
                                if (lastModified<=currentModified){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            }
                        } else if (syncRule.startsWith("S")) {
                            if (oldFileSize<=currentSize){
                                return null;
                            }
                            if (syncRule.endsWith("N")) {
                                if (currentModified<=lastModified){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            } else if (syncRule.endsWith("O")) {
                                if (lastModified<=currentModified){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            }
                        } else if (syncRule.startsWith("O")) {
                            if (lastModified<=currentModified){
                                return null;
                            }
                            if (syncRule.endsWith("B")) {
                                if (currentSize<=oldFileSize){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            } else if (syncRule.endsWith("S")) {
                                if (oldFileSize<=currentSize){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            }
                        } else if (syncRule.startsWith("N")) {
                            if (currentModified<=lastModified){
                                return null;
                            }
                            if (syncRule.endsWith("B")) {
                                if (currentSize<=oldFileSize){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            } else if (syncRule.endsWith("S")) {
                                if (oldFileSize<=currentSize){
                                    return null;
                                }else {
                                    return currentMd5;
                                }
                            }
                        } else {
                            return null;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    @Override
    public File getFileFromTarget(String sourceFilePath, FSClient target) {
        if (ClientAppContext.CONFIGS.CLIENT.getClientName().equals(target.getClientName()) && ClientAppContext.CONFIGS.CLIENT.getClientPassword().equals(target.getClientPassword())) {
            File sourceFile = new File(sourceFilePath);
            return sourceFile;
        }else {
            return null;
        }
    }
}
